package com.test.controller;

import com.test.pojo.Address;
import com.test.pojo.Users;
import com.test.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;

    public IAddressService getAddressService() {
        return addressService;
    }

    public void setAddressService(IAddressService addressService) {
        this.addressService = addressService;
    }

    private String[] province=new String[]{"安徽", "上海", "福建", "甘肃", "广东"};

    private String[][] city=new String[][]{
            { "安庆市", "蚌埠市", "巢湖市" } ,
            {  "上海市" } ,
            {  "福州市", "龙岩市", "南平市", "宁德市"  } ,
            {  "白银市", "定西市", "甘南藏族自治州市" } ,
            {  "潮州市", "佛山市" }
    };
    private String[][][] town=new String[][][]{

            {
                { "安庆市", "怀宁县", "潜山县", "宿松县", "太湖县", "桐城市" } ,
                { "蚌埠市", "固镇县", "怀远县", "五河县" } ,
                { "巢湖市", "含山县", "和县", "庐江县", "无为县" }
            },
            {
                    {"浦东区", "长宁区","宝山区","黄浦区"}
            },
            {
                    { "长乐市", "福清市", "福州市", "连江县" } ,
                    { "龙岩市", "长汀县", "连城县" } ,
                    { "光泽县", "建阳市", "建瓯市", "南平市", "浦城县", "邵武市"},
                     { "古田县", "宁德市", "屏南县", "寿宁县" }
            },
            {
                    {"白银市", "会宁县", "景泰县", "靖远县"},
                    {"定西县", "临洮县", "陇西县", "通渭县", "渭源县", "漳县", "岷县"},
                    {"迭部县", "合作市", "临潭县", "碌曲县", "玛曲县", "夏河县", "舟曲县", "卓尼县"}
            }
    };

    @RequestMapping("/addAddress")
    public String addAddress(Address address,String skuIdStr, HttpServletRequest request)
    {
        HttpSession session=request.getSession();
        Users user=(Users)session.getAttribute("loginUser");

        address.setUserId(user.getUserId());

        //将提交过来的省市区id 替换为对应的名称
        int pid=Integer.parseInt(address.getProvince());
        int cid=Integer.parseInt(address.getCity());
        int tid=Integer.parseInt(address.getTown());
        address.setProvince(province[pid]);
        address.setCity(city[pid][cid]);
        address.setTown(town[pid][cid][tid]);

        addressService.addAddress(address);

        return "redirect:/order/submitOrder?skuIdStr="+skuIdStr;


    }

}
